#!/bin/bash
#(c) Copyright Barry Kauler 2006,2007 www.puppylinux.com
#2007 Lesser GPL licence v2 (/usr/share/doc/legal/lgpl-2.1.txt)
# Contributors: Dougal, rerwin, pizzasgood, TazOC, Karl Godt
#               Jemimah, 01micko, mavrothal, rodin.s, zigbert, shinobar

#   https://www.x.org/wiki/Releases/
#-- Xorg X Server version should be 1.11+ --
#-- X11R7.6 (Released: 2010-12-20) should be the minimum version supported

[ "`whoami`" != "root" ] && exec sudo -A ${0} ${@}

export TEXTDOMAIN=xorgwizard
export OUTPUT_CHARSET=UTF-8

script=${0##*/}

[ ! $DISPLAY ] && CLI=1
case $script in
	xorgwizard-cli) CLI=1 ;;
	xorgwizard-automatic) AUTOMATIC=1 ;;
esac

ARCH=`uname -m`

. /etc/rc.d/PUPSTATE

#============================================================

function set_xorg_video_driver() {
	local vd
	[ -f /tmp/xorg.video.driver ] && read -r vd < /tmp/xorg.video.driver
	[ $1 ] && vd="$1"
	case $vd in
		auto) RSTR='#	Driver      "vesa" #card0driver' ;;
		"") return 1 ;;
		*) RSTR='	Driver      "'${vd}'" #card0driver' ;;
	esac
	echo $RSTR > /tmp/zz
	if [ -f /etc/X11/xorg.conf ] ; then
		sed -i "s|.*#card0driver.*|${RSTR}|" /etc/X11/xorg.conf
		rm -f /tmp/xorg.video.driver
	else
		echo -n "$vd" > /tmp/xorg.video.driver
	fi
}

function set_xorg_color_depth() {
	[ -f /tmp/xorg.depth ] && read -r XCD < /tmp/xorg.depth
	[ $1 ] && XCD="$1"
	case $XCD in
		16|24) ok=1 ;;
		remove) remove=1 ;;
		*) return 1 ;;
	esac
	if [ -f /etc/X11/xorg.conf ] ; then
		RSTR="		Depth       $XCD #screen0depth"
		if [ "$remove" = "1" ] ; then
			XCD=$(grep 'screen0depth' /etc/X11/xorg.conf | grep -o 'Depth.*' | tr -s ' ' | cut -d ' ' -f2)
			RSTR="#		Depth       $XCD #screen0depth"
		fi
		sed -i "s|.*#screen0depth.*|${RSTR}|" /etc/X11/xorg.conf
		rm -f /tmp/xorg.depth
	else
		echo -n "$XCD" > /tmp/xorg.depth
	fi
}

function set_xorg_screen_res() {
	[ -f /tmp/xorg.res ] && read -r RES < /tmp/xorg.res
	[ $1 ] && RES="$1"
	case $RES in
		*"x"*) ok=1 ;;
		*) return 1 ;;
	esac
	if [ -f /etc/X11/xorg.conf ] ; then
		RSTR="		Modes       \"${RES}\" #screen0modes"
		sed -i "s|.*#screen0modes.*|${RSTR}|" /etc/X11/xorg.conf
		rm -f /tmp/xorg.res
	else
		echo -n "$RES" > /tmp/xorg.res
	fi
	rm -f /etc/xrandrindex
}

function xorg_screen_res_dlg_stuff() {
	#- get default screen res
	if [ -f /etc/X11/xorg.conf ] ; then
		DEFAULT_RES=$(grep 'screen0modes' /etc/X11/xorg.conf | grep -v '#.*Modes.*' | cut -d '"' -f2)
		if [ "$DEFAULT_RES" = "" ] ; then
			DEFAULT_RES=$(report-video res)
		fi
	else
		DEFAULT_RES=$(report-video res)
	fi
	#- screen resolutions (edit this)
	RESOLUTIONS="1024x768
800x600
1280x1024
1366x768
1680x1050
1920x1080"
	#- messages
	export SCREEN_RES_TITLE=$(gettext 'Screen resolution')
	export SCREEN_RES_MSG1=$(gettext "Choose a reasonable resolution for your hardware. 
If Xorg does not apply the requested screen resolution then your hardware does not support it or you're probably missing firmware for your graphics card..")
	export SCREEN_RES_MSG2=$(gettext 'These are generic screen resolutions that will most likely work, be careful if using very ancient CRT monitors. If in doubt, choose 1024x768 or 1366x768. The settings will be stored in xorg.conf.')
}

#============================================================
#                  xorgwizard-automatic
#============================================================

if [ "$AUTOMATIC" = "1" ] ; then

# There are 2 possibilities:
# - xorg.conf.udev (highest priority - /var/local/xorg_udev)
# - xorg.conf-auto-pc (everything else)

if [ -f /var/local/xorg_udev ] ; then
	cp -fv /etc/X11/xorg.conf.udev /etc/X11/xorg.conf
	THE_END=1
else
	cp -fv /etc/X11/xorg.conf-auto-pc /etc/X11/xorg.conf
fi

set_xorg_video_driver
set_xorg_screen_res
set_xorg_color_depth

# xkbconfigmanager writes this when /etc/X11/xorg.conf is missing..
if [ -f /tmp/xorg.xkb.layout ] ; then
	read -r XKBLAYOUT < /tmp/xorg.xkb.layout
	xkbconfigmanager -q setlayout ${XKBLAYOUT}
	rm -f /tmp/xorg.xkb.layout
fi

if [ $THE_END ] ; then
	echo 
	echo "Xorg Wizard Automatic has just run."
	echo "If you are reading this, then you are at the console without X running."
	echo "The usual thing now is to type 'xwin' to start X..."
	exit ### exit ###
fi

TOUCHPAD=''
OLD3='Section "Monitor"'
[ "`grep 'Elantech' /proc/bus/input/devices`" ] && TOUCHPAD='Elantech'
[ "`grep 'Alps' /proc/bus/input/devices`" ] && TOUCHPAD='Alps'
[ "`grep 'Synaptics' /proc/bus/input/devices`" ] && TOUCHPAD='Synaptics'
if [ "$TOUCHPAD" ];then
 . /etc/rc.d/MODULESCONFIG #ADDLIST
 if [ "`lsmod | grep '^evdev'`" = "" ];then
   if ! ( grep 'ADDLIST' /etc/rc.d/MODULESCONFIG | grep evdev ) ; then
     REGEXt0="s%^ADDLIST=.*%ADDLIST='${ADDLIST} evdev '%"
     sed -i -e "$REGEXt0" /etc/rc.d/MODULESCONFIG #so evdev module will load next bootup.
     modprobe evdev
   fi
 fi
 #110913 create basic section...
 REGEXt1='s%.*#loadsynaptics%	Load "synaptics" #loadsynaptics%'
 REGEXt2='s%.*#serverlayoutsynaptics%	InputDevice "Synaptics Mouse" "AlwaysCore" #serverlayoutsynaptics%'
 NEW3='Section "InputDevice"\
	Identifier "Synaptics Mouse"\
	Driver "synaptics"\
	Option "SHMConfig" "on"\
EndSection\
\
Section "Monitor"'
 REGEXt3="s%${OLD3}%${NEW3}%"
 sed -i -e "$REGEXt1" -e "$REGEXt2" -e "$REGEXt3" /etc/X11/xorg.conf
fi

echo 
echo "Xorg Wizard Automatic has just run."
echo "If you are reading this, then you are at the console without X running."
echo "The usual thing now is to type 'xwin' to start X..."
exit

fi

#END OF xorgwizard-automatic
#===========================


#============================================================
#                      xorgwizard-cli
#============================================================

if [ "$CLI" = "1" ] ; then

if [ -f /etc/X11/xorg.conf ] ; then
	USING_XORG_GENERIC="$(cat /etc/X11/xorg.conf | grep '#card0driver' | grep -v '#.*Driver')"
elif [ -f /tmp/xorg.video.driver ] ; then
	read -r xvid_drv < /tmp/xorg.video.driver
fi

function cli_dlg_input() {
	# $1:title ; $2:msg
	dialog --title "$1" --clear --inputbox "$2" 0 0 >/dev/tty 2>/tmp/in$$
	if [ $? -eq 0 ] ; then
		echo -n "$(cat /tmp/in$$)"
		rm -f /tmp/in$$
	fi
}

function cli_xorg_video_driver() {
	local VD SPECIFY
	dialog --title "$(gettext 'Xorg video driver')" \
		--ok-label "$(gettext 'Select')" --cancel-label "$(gettext 'Cancel')" \
		--menu "$(gettext 'Automatic is recommended. Choose a generic driver or specify the driver of your preference if you know what you are doing.')." 0 0 0 \
		"1)" "Automatic" \
		"2)" "vesa (Generic VESA video driver)"  \
		"3)" "modesetting (Driver for KMS devices)" \
		"4)" "fbdev (Framebuffer device based X server)" \
		"*" "Specify..." >/dev/tty 2>/tmp/xorg.cli.res
	if [ $? -eq 0 ] ; then
		read -r ZCHOICE < /tmp/xorg.cli.res
		case $ZCHOICE in
			"1)") set_xorg_video_driver auto ; VD=Automatic ;;
			"2)") set_xorg_video_driver vesa ; VD=vesa ;;
			"3)") set_xorg_video_driver modesetting ; VD=modesetting ;;
			"4)") set_xorg_video_driver fbdev ; VD=fbdev ;;
			"*") SPECIFY=1 ;;
		esac
		if [ $SPECIFY ] ; then
			VD=$(cli_dlg_input "$(gettext 'Video Driver')" "$(gettext 'Enter the xorg video driver you want to use:')")
			[ "$VD" ] && set_xorg_video_driver $VD
		fi
		if [ "$VD" ] ; then
			dialog --msgbox "$(printf "$(gettext "Xorg Video driver set to %s")" "$VD")" 7 35
		fi
	fi
	rm -f /tmp/xorg.cli.res
}

function cli_xorg_color_depth() {
	local DEPTH
	dialog --title "$(gettext 'Color bit depth.')" \
		--ok-label "$(gettext 'Select')" --cancel-label "$(gettext 'Cancel')" \
		--menu "$(gettext 'You might want to skip this step. Choose "16 bit" if you are having problems and your video hardware is very old... or if you already know that your hardware works better with a 16 color bit depth')." 0 0 0 \
		"1)" "unset (let xorg decide)" \
		"2)" "24 bit" \
		"3)" "16 bit"  \
		 >/dev/tty 2>/tmp/xorg.cli.res
	if [ $? -eq 0 ] ; then
		read -r ZCHOICE < /tmp/xorg.cli.res
		case $ZCHOICE in
			"1)") DEPTH="remove" ;;
			"2)") DEPTH="24" ;;
			"3)") DEPTH="16" ;;
		esac
		set_xorg_color_depth $DEPTH
		if [ "$DEPTH" = "remove" ] ; then
			dialog --msgbox "$(gettext 'Color bit depth UNSET')" 7 35
		else
			dialog --msgbox "$(printf "$(gettext "Color bit depth set to %s")" "$DEPTH")" 7 35
		fi
	fi
	rm -f /tmp/xorg.cli.res
}

function cli_xorg_screen_res() {
	local CHOSEN_RES SPECIFY
	xorg_screen_res_dlg_stuff #sets $DEFAULT_RES $RESOLUTIONS, etc..
	if [ "$DEFAULT_RES" ]  ; then
		CLI_RESOLUTIONS="$DEFAULT_RES
$(echo "$RESOLUTIONS" | grep -v "^${DEFAULT_RES}$")"
	else
		CLI_RESOLUTIONS="$RESOLUTIONS"
	fi
	ITEMCOUNT=$(echo "$CLI_RESOLUTIONS" | wc -l)
	x=1
	(
	echo 'dialog --title "'${SCREEN_RES_TITLE}'" --ok-label "'$(gettext 'Select')'" --cancel-label "'$(gettext 'Cancel')'" '${DEFAULT_NO}' \
--menu "'${SCREEN_RES_MSG1}'

'${SCREEN_RES_MSG2}'" 0 0 0 \'
	echo "$CLI_RESOLUTIONS" | while read line ; do
			echo -n "\"${x})\" \"$line\" "
			x=$((x+1))
	done
	echo -n ' "*" "Specify..."'
	echo '>/dev/tty 2>/tmp/xorg.cli.res'
	echo 'exit $?'
	) > /tmp/xorg.cli
	sh /tmp/xorg.cli
	retval=$?
	if [ $retval -eq 0 ] ; then
		if [ $ITEMCOUNT -gt 1 ] ; then
			choice=$(< /tmp/xorg.cli.res)
			choice=${choice%\)}
			if [ "$choice" = "*" ] ; then
				"* Specify..."
				CHOSEN_RES=$(cli_dlg_input "$(gettext 'Screen resolution')" "$(gettext 'Enter the desired screen resolution:')")
			else
				CHOSEN_RES=$(echo "$CLI_RESOLUTIONS" | sed -n "${choice}p" )
			fi
		fi
	fi
	case $CHOSEN_RES in
		"") ok=1 ;;
		*)
			if set_xorg_screen_res ${CHOSEN_RES} ; then
				dialog --msgbox "$(printf "$(gettext "Screen resolution set to %s")" "$CHOSEN_RES")" 7 35
			else
				dialog --msgbox "$(printf "$(gettext "%s rejected... probably not a valid screen resolution")" "$CHOSEN_RES")" 7 35
			fi
			;;
	esac
	rm -f /tmp/xorg.cli /tmp/xorg.cli.res
}

if [ $PUPMODE -eq 5 -a ! -f /var/tmp/xorgwiz.1st ] ; then
	TITLE_0=$(gettext 'Welcome')
	MSG_NO_CONFIG=$(gettext 'Do not configure anything (just continue)')
	echo -n > /var/tmp/xorgwiz.1st
else
	TITLE_0=$(gettext 'Xorg Wizard')
	MSG_NO_CONFIG=$(gettext 'Done')
fi

if [ -x /opt/xorg_alt/usr/bin/Xorg ] ; then
	#add version in /opt/xorg_alt/usr/lib/xorg/version
	XORGVER2="`/opt/xorg_alt/usr/bin/Xorg -version 2>&1 | grep '^X\.Org X Server' | sed 's|.* ||'`"
	[ ! "$XORGVER2" ] && XORGVER2="$(cat /opt/xorg_alt/usr/lib/xorg/version)"
	MSG_ALTXORG="$(gettext 'Use alternative Xorg server') (${XORGVER2}).."
	MSG_3=$(gettext 'As a last resort you can use an alternative Xorg server version and drivers.')
fi

XORGVER="`Xorg -version 2>&1 | grep '^X\.Org X Server' | sed 's|.* ||'`"
MSG_1="$(gettext 'Using') Xorg server ${XORGVER}"
MSG_2="$(gettext 'If you are having problems with Xorg then you might want to set the options below..') ${MSG_3}"
MSG_4="$(gettext 'New xorg.conf (reset)')"
MSG_SET_SR=$(gettext 'Set screen resolution...')
MSG_SET_CBD=$(gettext 'Set color bith depth...')
MSG_SET_VDRIVER="$(gettext 'Set video driver...')"

(
echo -n 'dialog --title "'${TITLE_0}'" --ok-label "'$(gettext 'OK')'" --no-cancel \
	--menu "'${MSG_1}'

'${MSG_2}'" 0 0 0 \
	"-" "'${MSG_NO_CONFIG}'" \
	"1)" "'${MSG_SET_VDRIVER}'" \
	"2)" "'${MSG_SET_SR}'" \
	"3)" "'${MSG_SET_CBD}'" \
	"4)" "'${MSG_4}'"'
	if [ -x /opt/xorg_alt/usr/bin/Xorg ] ; then
		echo -n ' \
	"5)" "'${MSG_ALTXORG}'"'
	fi
	echo ' >/dev/tty 2>/tmp/xorg.cli.res'
	echo 'exit $?'
) > /tmp/xorg.cli
sh /tmp/xorg.cli
retval=$?
read -r XCHOICE < /tmp/xorg.cli.res
rm -f /tmp/xorg.cli /tmp/xorg.cli.res
[ $retval -eq 0 ] || exit

case $XCHOICE in
	"-") exit ;;
	"1)") cli_xorg_video_driver ;;
	"2)") cli_xorg_screen_res   ;;
	"3)") cli_xorg_color_depth  ;;
	"4)")
		dialog --title "$(gettext 'reset xorg.conf')" \
			--no-label "$(gettext 'Cancel')" --yes-label "$(gettext 'Confirm')" \
			--yesno "$(gettext 'All custom settings (if any) will be lost.

xorgwizard-automatic will run so the new xorg.conf is usable')" 0 0
		if [ $? -eq 0 ] ; then
			rm -f /tmp/xorg.res /tmp/xorg.depth /tmp/xorg.video.driver
			xorgwizard-automatic &>/dev/null
			dialog --msgbox "$(gettext "xorg.conf has been reset. The command to start X is 'xwin'...")" 7 35
		fi
		;;
	"5)")
		# * This requires a custom Xorg server package ready to be used
		#   and it must be placed in /var/xorg_alt
		# * It must be already tested and with all the needed drivers and dependencies..
		dialog --title "$(gettext 'Alternative Xorg server')" \
			--no-label "$(gettext 'No')" --yes-label "$(gettext 'Yes')" \
			--yesno "Xorg ${XORGVER} -> ${XORGVER2}

$(gettext 'By using this option the current Xorg server will be replaced permanently..')" 0 0
		if [ $? -eq 0 ] ; then
			#now replace Xorg server
			rm -rf /usr/lib/xorg
			rm -rf /usr/share/X11/xorg.conf.d
			rm -f /lib/udev/rules.d/*xorg*
			cp -rf --remove-destination /opt/xorg_alt/* /
			rm -rf /opt/xorg_alt
			dialog --msgbox "$(printf "$(gettext "Now using xorg version: %s")" "$XORGVER2")" 7 35
		fi
		;;
	*) exit #any other choice
esac

exec $0 #reenter this script...

fi

#END OF xorgwizard-cli
#=====================


#============================================================
#                        xorgwizard
#============================================================

help(){
 case "$1" in
	resolution)
		HEADING="`gettext 'Screen Resolution'`"
		msg="$SCREEN_RES_MSG1

$(gettext 'If you know what you do, you can also type a screen resolution, then click on Ok.')"
		;;
	color)
		HEADING="`gettext 'Screen Resolution'`"
		msg="$(gettext 'This is specially for old hardware/old xorg versions..')"
		;;
	dpi)
		HEADING="`gettext 'Font DPI (Dots Per Inch)'`"
		msg="$(gettext 'You can specify it manually in the Xorg configuration (recommended solution), but it may not work with buggy drivers.

This settings is stored in ~/.Xresources and is used by FontConfig')

Recommended value: 96"
		;;
	esac
	/usr/lib/gtkdialog/box_ok "$HEADING" info "$msg" &
}
export -f help

#---

. /etc/DISTRO_SPECS #120723
. /etc/rc.d/BOOTCONSTRAINED #120723

#--------------------
function setscreenres_gui() {
	xorg_screen_res_dlg_stuff #sets $DEFAULT_RES $RESOLUTIONS, etc..
	GUI_RESOLUTIONS="<item>$DEFAULT_RES</item>
$(echo "$RESOLUTIONS" | grep -v "^${DEFAULT_RES}$" | sed -e 's|^|<item>|' -e 's|$|</item>|')"

	#DEFAULT_CD=$(grep 'screen0depth' /etc/X11/xorg.conf | grep -o 'Depth.*' | tr -s ' ' | cut -d ' ' -f2)
	COLOR_DEPTHS="<item></item><item>24</item><item>16</item><item>remove</item>"
	#--
	#CURRENT_DPI=$(xdpyinfo  | grep dots | tr -s ' ' | cut -f3 -d ' ' | cut -f 1 -d 'x')
	CURRENT_DPI=$(grep 'Xft.dpi' /root/.Xresources  | sed 's/.* //')
	[ -z $CURRENT_DPI ] && CURRENT_DPI=96
	DPI_LIST="54!60!66!72!78!84!90!96!102!108!114"
	DPI_COMBO="<item>$CURRENT_DPI</item>
$(echo "$DPI_LIST" | tr '!' '\n' | grep -v "^${CURRENT_DPI}$" | sed -e 's|^|<item>|' -e 's|$|</item>|')"
	#--
	export RES_WINDOW='<window title="'${SCREEN_RES_TITLE}'" icon-name="gtk-preferences" window-position="1">
<vbox>
'$(/usr/lib/gtkdialog/xml_info fixed graphics.svg 48 "$(gettext "Set screen resolution")")'
  <frame Screen Resolution>
  <text space-fill="true"><label>'${SCREEN_RES_MSG2}'</label></text>
  <vbox space-expand="false" space-fill="false">
    <hbox>
      <combobox>
        '${GUI_RESOLUTIONS}'
        <variable>CHOSEN_RES</variable>
        <visible>enabled</visible>
       </combobox>
        <button>
          '"`/usr/lib/gtkdialog/xml_button-icon info mini`"'
          <action>help resolution</action>
        </button>
     </hbox>
   </vbox>
   </frame>

  <frame '$(gettext 'Color Bit Depth')'>
   <text space-fill="true"><label>'$(gettext "You probably don't want to set this..")'</label></text>
   <vbox space-expand="false" space-fill="false">
    <hbox>
      <combobox>
        '${COLOR_DEPTHS}'
        <variable>CHOSEN_CD</variable>
       </combobox>
       <button>
          '"`/usr/lib/gtkdialog/xml_button-icon info mini`"'
          <action>help color</action>
       </button>
     </hbox>
   </vbox>
  </frame>

  <frame '$(gettext 'Font DPI')'>
   <vbox space-expand="false" space-fill="false">
    <hbox>
      <combobox>
        '${DPI_COMBO}'
        <variable>CHOSEN_DPI</variable>
       </combobox>
       <button>
          '"`/usr/lib/gtkdialog/xml_button-icon info mini`"'
          <action>help dpi</action>
       </button>
     </hbox>
   </vbox>
  </frame>

	<hbox>
		<button>
			'$(/usr/lib/gtkdialog/xml_button-icon ok)'
			<label>'$(gettext 'OK')'</label>
			<action type="exit">OK</action>
		</button>
		<button>
			'$(/usr/lib/gtkdialog/xml_button-icon cancel)'
			<label>'$(gettext 'Cancel')'</label>
			<action type="exit">Cancel</action>
		</button>
	</hbox>
 </vbox>
</window>'

	. /usr/lib/gtkdialog/xml_info gtk
	eval "$(gtkdialog --center --program RES_WINDOW)"

	[ "$EXIT" != "OK" ] && exit
	if [ "$CHOSEN_RES" != "" ] ; then
	    if [ "$CHOSEN_RES" != "$DEFAULT_RES" -o -f /etc/xrandrindex ]; then
		rm -f /etc/xrandrindex
		set_xorg_screen_res ${CHOSEN_RES}
		restart=1
	    fi
	fi
	if [ "$CHOSEN_CD" != "" -a "$CHOSEN_CD" != "$DEFAULT_CD" ] ; then
		set_xorg_color_depth ${CHOSEN_CD}
		restart=1
	fi
	if [ "$CHOSEN_DPI" != "" -a "$CHOSEN_DPI" != "$CURRENT_DPI" ] ; then
		sed -i '/Xft.dpi/d' $HOME/.Xresources
		echo "Xft.dpi: $CHOSEN_DPI" >> $HOME/.Xresources
		restart=1
	fi
	[ $restart ] || exit
	/usr/lib/gtkdialog/box_yesno --yes-first "$(gettext 'Xorg Video Wizard')" "$(gettext 'For the changes to effect you must restart X... Would you like to restart X now?')"
	[ $? -eq 0 ] && restartwm
}
if [ "$1" = "setscreenres" -o "$script" = "setscreenres" ] ; then
	setscreenres_gui
	exit
fi
#----------------


GLXGEARSBUTTON='<button space-expand="false" space-fill="false">
  '"`/usr/lib/gtkdialog/xml_button-icon /usr/share/pixmaps/puppy/graph.svg`"'
  <label>'$(gettext "glxgears...")'</label>
  <action>xterm -hold -e glxgears &</action>
 </button>'

REPORTVIDEOBUTTON='<button space-expand="false" space-fill="false">
  '"`/usr/lib/gtkdialog/xml_button-icon /usr/share/pixmaps/puppy/clipboard.svg`"'
  <label>'$(gettext "report-video...")'</label>
  <action>gtk_text_info report-video &</action>
 </button>'


if which nvidia-settings &>/dev/null && NRATE=$(nvidia-settings -q RefreshRate | grep '[0-9]') ; then
	NVIDIABUTTON='<button space-expand="false" space-fill="false" tooltip-text="'$(gettext 'Launch the NVIDIA X Server Settings.')'">
  '"`/usr/lib/gtkdialog/xml_button-icon /usr/share/pixmaps/nvidia48x32.png`"'
  <label>'$(gettext "nvidia-settings")'</label>
  <action>nvidia-settings &</action>
 </button>'
 USING_NVIDIA=1
fi

function gui_opt() { #exit icon msg
	echo '
        <hbox space-expand="true" space-fill="true">
          <text xalign="0" use-markup="true" space-expand="true" space-fill="true"><label>"'"$3"'"</label></text>
          <vbox space-expand="false" space-fill="false">
            <button>
              '"`/usr/lib/gtkdialog/xml_button-icon $2 big`"'
              <action>'"$1"'</action>
            </button>
          </vbox>
        </hbox>'
}

export MAIN1='
<window title="'$(gettext 'Xorg Video Wizard')'" image-name="/usr/share/pixmaps/puppy/graphics_xorg.svg">
<vbox space-expand="true" space-fill="true">
  <notebook labels="'$(gettext 'Screen')'|'$(gettext 'Advanced')'" space-expand="true" space-fill="true">
    <vbox space-expand="true" space-fill="true">
      <frame '$(gettext 'Screen tuning')'>
      '"`/usr/lib/gtkdialog/xml_info fixed graphics.svg 60 "$(gettext '<b>Screen tuning</b> depends on the loaded video/graphics driver, so if correct resolution is not there, check the advanced tab...')"`"'
        '$(gui_opt EXIT:19 graphics_xorg.svg "$(gettext '<b>Screen resolution / Color depth (xorg.conf)</b>
Guarantees a proper X startup..')")'
        <hseparator></hseparator>
        '$(gui_opt EXIT:18 screen_resolution.svg "$(gettext '<b>Screen resolution (xrandr)</b>
How many vertical and horizontal pixels fits your screen..')")'
        <hseparator></hseparator>
        '$(gui_opt EXIT:17 screen_calibration.svg "$(gettext '<b>Monitor gamma calibration</b>
Adjust monitor colors including screen brightness..')")'
       </frame>
    </vbox>
    <vbox space-expand="true" space-fill="true">
      <frame '$(gettext 'Manage xorg.conf')'>
        '"`/usr/lib/gtkdialog/xml_info fixed graphics.svg 60 "$(gettext 'The behaviour of Xorg is controlled by a configuration file, /etc/X11/xorg.conf. This was generated automatically at the first boot, but you may now edit it manually.')"`"'
        '$(gui_opt EXIT:15 edit.svg "$(gettext 'You can <b>manually edit</b> /etc/X11/xorg.conf, but note that you will need to exit from X afterward then restart X (see Shutdown menu). Click button to edit:')")'
        <hseparator></hseparator>
        '$(gui_opt EXIT:13 screen_xy.svg "$(gettext '<b>X/Y correction</b>
If the screen is displaced or the width/height are wrong. This will modify the existing xorg.conf file. <b>Use with caution!</b>')")'
      </frame>
  
    </vbox>
  </notebook>
  <hbox space-expand="false" space-fill="true">
    '${NVIDIABUTTON}'
    '${UPGRADEBUTTON}'
    '${REPORTVIDEOBUTTON}'
    '${GLXGEARSBUTTON}'
    <text space-expand="true" space-fill="false"><label>" "</label></text>
    <button space-expand="false" space-fill="false">
      <label>'$(gettext "Quit")'</label>
      '"`/usr/lib/gtkdialog/xml_button-icon quit`"'
      <action>EXIT:quit</action>
    </button>
  </hbox>
</vbox>
</window>'


. /usr/lib/gtkdialog/xml_info gtk #build bg_pixmap for gtk-theme
eval "$(gtkdialog --center --program=MAIN1)"

case $EXIT in
  13) #xvidtune
   /usr/lib/gtkdialog/box_ok "$(gettext 'Xvidtune IMPORTANT HELP')" info "$(gettext "If you click the <b>Show</b> button, the adjusted settings will be what you want permanently. After hitting the <b>Quit</b> button, you will be given one last chance not to make the change permanent.")" "$(gettext 'Thus:')" "$(gettext "<b>Show</b> to record the settings (for later inclusion into xorg.conf)")" "$(gettext "<b>Quit</b> to exit Xvidtune.")" " " "$(gettext 'Please note that Xvidtune does not work with all video hardware, meaning that changing the settings will cause no change on the screen.')" &
   XMSGPID=$!
   MODELINE0="`xvidtune | grep '^"[0-9]'`" #'geanyfix.
   pupkill $XMSGPID
   if [ ! "$MODELINE0" = "" ];then
    xmessage -bg orange -title "$(gettext 'Xvidtune: Modeline')" -buttons Write:10,Quit:11 "$(gettext 'The new modeline is:')
$MODELINE0

$(gettext 'Note, you will have to restart X for it to take effect. If it messes')
`gettext \"up X, edit from commandline 'mp /etc/X11/xorg.conf' and comment-out\"`
`gettext \"the 'UseModes' line (do not delete it) in the Monitor section.\"`

`gettext \"To insert this into /etc/X11/xorg.conf, click 'Write' button...\"`
`gettext \"To exit without changing xorg.conf, click 'Quit' button...\"`"
    if [ $? -eq 10 ];then
     PATTERNA="s/.*#modes0modeline0/ ModeLine $MODELINE0 #modes0modeline0/g"
     cat /etc/X11/xorg.conf | sed -e "$PATTERNA" > /tmp/xorg.conf.new
     sync
     cat /tmp/xorg.conf.new | sed -e 's/#.*UseModes/UseModes/g' > /etc/X11/xorg.conf
     sync
    fi
   fi
   exit
   ;;
  15) exec defaulttexteditor /etc/X11/xorg.conf ;; #edit xorg.conf
  17) exec xgamma-gui  ;;#xgamma-gui
  18) #resolution changer
    [ "$USING_NVIDIA" ] && exec nvidia-settings
    exec xrandrshell
    ;;
  19) #resolution changer
    [ "$USING_NVIDIA" ] && exec nvidia-settings
	setscreenres_gui
	;;
esac

### END ###
